﻿using System;
using System.IO;
using System.Net;
//
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace Common
{
    /// <summary>
    /// 加密解密处理类
    /// </summary>
    public class EncryptionHelper
    {
        #region 对称加密

        /// <summary>
        /// Base64加密、解密
        /// </summary>
        public static class Base64Helper
        {
            /// <summary>
            /// 加密
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public static string Encrypt(string code)
            {
                string encode = "";
                byte[] bytes = Encoding.UTF8.GetBytes(code);
                try
                {
                    encode = Convert.ToBase64String(bytes);
                }
                catch
                {
                    encode = code;
                }
                return encode;
            }

            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public static string Decrypt(string code)
            {
                string decode = "";
                byte[] bytes = Convert.FromBase64String(code);
                try
                {
                    decode = Encoding.UTF8.GetString(bytes);
                }
                catch
                {
                    decode = code;
                }
                return decode;
            }
        }

        /// <summary>
        /// 数据加密算法
        /// </summary>
        public static class DESHelper
        {
            public const string DesDefaultKey = "EDAZQYWC";
            public static string Encrypt(string encryptString)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(encryptString);

                byte[] a = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                des.Key = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);//将第一个参数转换为十六进制数,长度为2,不足前面补0
                }
                return ret.ToString();
            }

            public static string Decrypt(string encryptString)
            {
                if (string.IsNullOrEmpty(encryptString))
                    return string.Empty;
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                byte[] inputByteArray = new byte[encryptString.Length / 2];
                for (int x = 0; x < encryptString.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(encryptString.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                des.Key = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                StringBuilder ret = new StringBuilder();

                return System.Text.Encoding.GetEncoding("UTF-8").GetString(ms.ToArray());
            }

            /// <summary>
            /// DES加密
            /// </summary>
            /// <param name="encryptString">待加密的密文</param>
            /// <param name="encryptKey">密匙（8位）</param>
            /// <returns></returns>
            public static string Encrypt(string encryptString, string encryptKey)
            {
                string returnValue;
                try
                {
                    string defaultKey = "edoctorBaseFrame";
                    if (string.IsNullOrEmpty(encryptKey))
                    {
                        encryptKey = defaultKey;
                    }
                    if (encryptKey.Length < 8)
                    {
                        encryptKey = encryptKey + defaultKey.Substring(0, 8 - encryptKey.Length);
                    }
                    if (encryptKey.Length > 8)
                    {
                        encryptKey = encryptKey.Substring(0, 8);
                    }
                    byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                    DESCryptoServiceProvider dES = new DESCryptoServiceProvider();
                    byte[] byteEncrypt = Encoding.UTF8.GetBytes(encryptString);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, dES.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                    cryptoStream.Write(byteEncrypt, 0, byteEncrypt.Length);
                    cryptoStream.FlushFinalBlock();
                    returnValue = Convert.ToBase64String(memoryStream.ToArray());

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return returnValue;
            }

            /// <summary>
            /// DES解密
            /// </summary>
            /// <param name="decryptString">密文</param>
            /// <param name="decryptKey">密匙（8位）</param>
            /// <returns></returns>
            public static string Decrypt(string decryptString, string decryptKey)
            {
                string returnValue;
                try
                {
                    string defaultKey = "edoctorBaseFrame";
                    if (string.IsNullOrEmpty(decryptKey))
                    {
                        decryptKey = defaultKey;
                    }
                    if (decryptKey.Length < 8)
                    {
                        decryptKey = decryptKey + defaultKey.Substring(0, 8 - decryptKey.Length);
                    }
                    if (decryptKey.Length > 8)
                    {
                        decryptKey = decryptKey.Substring(0, 8);
                    }
                    byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                    DESCryptoServiceProvider dES = new DESCryptoServiceProvider();
                    byte[] byteDecryptString = Convert.FromBase64String(decryptString);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, dES.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);

                    cryptoStream.Write(byteDecryptString, 0, byteDecryptString.Length);

                    cryptoStream.FlushFinalBlock();

                    returnValue = Encoding.UTF8.GetString(memoryStream.ToArray());

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return returnValue;

            }

            /// <summary>
            /// 加密方法
            /// </summary>
            /// <param name="pToEncrypt"></param>
            /// <param name="sKey"></param>
            /// <returns></returns>
            public static string Encrypt1(string pToEncrypt)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中
                //原来使用的UTF8编码，我改成Unicode编码了，不行
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                //byte[]  inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); 

                //建立加密对象的密钥和偏移量
                //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
                //使得输入密码必须输入英文文本
                des.Key = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                //创建其支持存储区为内存的流
                MemoryStream ms = new MemoryStream();
                //将数据流链接到加密转换的流
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                //Write  the  byte  array  into  the  crypto  stream 
                //(It  will  end  up  in  the  memory  stream) 
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                //用缓冲区的当前状态更新基础数据源或储存库，随后清除缓冲区
                cs.FlushFinalBlock();
                //Get  the  data  back  from  the  memory  stream,  and  into  a  string 
                byte[] EncryptData = (byte[])ms.ToArray();
                return System.Convert.ToBase64String(EncryptData, 0, EncryptData.Length);
            }
            /// <summary>
            /// 解密方法
            /// </summary>
            /// <param name="pToDecrypt"></param>
            /// <param name="sKey"></param>
            /// <returns></returns> 
            public static string Decrypt1(string pToDecrypt)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //Put  the  input  string  into  the  byte  array 
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);

                //建立加密对象的密钥和偏移量，此值重要，不能修改
                des.Key = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(DesDefaultKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                //Flush  the  data  through  the  crypto  stream  into  the  memory  stream 
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
        }

        /// <summary>
        /// DES3重安全加密算法处理类
        /// </summary>
        public static class DES3Helper
        {
            /// <summary>
            /// 3DES 加密
            /// </summary>
            /// <param name="encryptString">待加密密文</param>
            /// <param name="encryptKey1">密匙1(长度必须为8位)</param>
            /// <param name="encryptKey2">密匙2(长度必须为8位)</param>
            /// <param name="encryptKey3">密匙3(长度必须为8位)</param>
            /// <returns></returns>
            public static string Encrypt(string encryptString, string encryptKey1, string encryptKey2, string encryptKey3)
            {

                string returnValue;
                try
                {
                    returnValue = DESHelper.Encrypt(encryptString, encryptKey3);
                    returnValue = DESHelper.Encrypt(returnValue, encryptKey2);
                    returnValue = DESHelper.Encrypt(returnValue, encryptKey1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return returnValue;

            }
            /// <summary>
            /// 3DES 解密
            /// </summary>
            /// <param name="decryptString">待解密密文</param>
            /// <param name="decryptKey1">密匙1(长度必须为8位)</param>
            /// <param name="decryptKey2">密匙2(长度必须为8位)</param>
            /// <param name="decryptKey3">密匙3(长度必须为8位)</param>
            /// <returns></returns>
            public static string Decrypt(string decryptString, string decryptKey1, string decryptKey2, string decryptKey3)
            {

                string returnValue;
                try
                {
                    returnValue = DESHelper.Decrypt(decryptString, decryptKey1);
                    returnValue = DESHelper.Decrypt(returnValue, decryptKey2);
                    returnValue = DESHelper.Decrypt(returnValue, decryptKey3);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return returnValue;
            }


        }


        /// <summary>   
        /// AES对称加密算法类   
        /// </summary> 
        public class AESHelper
        {

            public const string AesDefaultKey = "eDoctorCorpQiyeWechatAES";
            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="encryptString">待加密的密文</param>
            /// <returns></returns>
            public static string Encrypt(string encryptString)
            {
                string returnValue;
                byte[] temp = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
                Rijndael AESProvider = Rijndael.Create();
                try
                {
                    byte[] byteEncryptString = Encoding.UTF8.GetBytes(encryptString);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, AESProvider.CreateEncryptor(Encoding.UTF8.GetBytes(AesDefaultKey), temp), CryptoStreamMode.Write);
                    cryptoStream.Write(byteEncryptString, 0, byteEncryptString.Length);
                    cryptoStream.FlushFinalBlock();
                    returnValue = Convert.ToBase64String(memoryStream.ToArray());
                }

                catch (Exception ex)
                {
                    throw ex;
                }

                return returnValue;

            }
            /// <summary>
            ///AES 解密
            /// </summary>
            /// <param name="decryptString">待解密密文</param>
            /// <returns></returns>
            public static string Decrypt(string decryptString)
            {
                string returnValue = "";
                byte[] temp = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
                Rijndael AESProvider = Rijndael.Create();
                try
                {
                    byte[] byteDecryptString = Convert.FromBase64String(decryptString);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, AESProvider.CreateDecryptor(Encoding.UTF8.GetBytes(AesDefaultKey), temp), CryptoStreamMode.Write);
                    cryptoStream.Write(byteDecryptString, 0, byteDecryptString.Length);
                    cryptoStream.FlushFinalBlock();
                    returnValue = Encoding.UTF8.GetString(memoryStream.ToArray());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return returnValue;
            }


            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="encryptString">待加密的密文</param>
            /// <param name="encryptKey">加密密匙</param>
            /// <returns></returns>
            public static string Encrypt(string encryptString, string encryptKey)
            {
                string returnValue;
                byte[] temp = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
                Rijndael AESProvider = Rijndael.Create();
                try
                {
                    string defaultKey = "eDoctorCorpQiyeWechatAES";
                    if (string.IsNullOrEmpty(encryptKey))
                    {
                        encryptKey = defaultKey;
                    }
                    if (encryptKey.Length < 24)
                    {
                        encryptKey = encryptKey + defaultKey.Substring(0, 24 - encryptKey.Length);
                    }
                    if (encryptKey.Length > 24)
                    {
                        encryptKey = encryptKey.Substring(0, 24);
                    }
                    byte[] byteEncryptString = Encoding.UTF8.GetBytes(encryptString);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, AESProvider.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                    cryptoStream.Write(byteEncryptString, 0, byteEncryptString.Length);
                    cryptoStream.FlushFinalBlock();
                    returnValue = Convert.ToBase64String(memoryStream.ToArray());
                }

                catch (Exception ex)
                {
                    throw ex;
                }

                return returnValue;

            }
            /// <summary>
            ///AES 解密
            /// </summary>
            /// <param name="decryptString">待解密密文</param>
            /// <param name="decryptKey">解密密钥</param>
            /// <returns></returns>
            public static string Decrypt(string decryptString, string decryptKey)
            {
                string returnValue = "";
                byte[] temp = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
                Rijndael AESProvider = Rijndael.Create();
                try
                {
                    string defaultKey = "eDoctorCorpQiyeWechatAES";
                    if (string.IsNullOrEmpty(decryptKey))
                    {
                        decryptKey = defaultKey;
                    }
                    if (decryptKey.Length < 24)
                    {
                        decryptKey = decryptKey + defaultKey.Substring(0, 24 - decryptKey.Length);
                    }
                    if (decryptKey.Length > 24)
                    {
                        decryptKey = decryptKey.Substring(0, 24);
                    }
                    byte[] byteDecryptString = Convert.FromBase64String(decryptString);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, AESProvider.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);
                    cryptoStream.Write(byteDecryptString, 0, byteDecryptString.Length);
                    cryptoStream.FlushFinalBlock();
                    returnValue = Encoding.UTF8.GetString(memoryStream.ToArray());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return returnValue;
            }


            /// <summary>
            /// 获取密钥
            /// </summary>
            private static string Key
            {
                get
                {
                    return "abcdef1234567890";    ////必须是16位
                }
            }
            //默认密钥向量 
            private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            /// <summary>
            /// AES加密算法
            /// </summary>
            /// <param name="plainText">明文字符串</param>
            /// <returns>将加密后的密文转换为Base64编码，以便显示</returns>
            public static string AESEncrypt(string plainText)
            {
                //分组加密算法
                SymmetricAlgorithm des = Rijndael.Create();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组 
                //设置密钥及密钥向量
                des.Key = Encoding.UTF8.GetBytes(Key);
                des.IV = _key1;
                byte[] cipherBytes = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cipherBytes = ms.ToArray();//得到加密后的字节数组
                        cs.Close();
                        ms.Close();
                    }
                }
                return Convert.ToBase64String(cipherBytes);
            }
            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="cipherText">密文字符串</param>
            /// <returns>返回解密后的明文字符串</returns>
            public static string AESDecrypt(string showText)
            {
                byte[] cipherText = Convert.FromBase64String(showText);
                SymmetricAlgorithm des = Rijndael.Create();
                des.Key = Encoding.UTF8.GetBytes(Key);
                des.IV = _key1;
                byte[] decryptBytes = new byte[cipherText.Length];
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        cs.Read(decryptBytes, 0, decryptBytes.Length);
                        cs.Close();
                        ms.Close();
                    }
                }
                return Encoding.UTF8.GetString(decryptBytes).Replace("\0", "");   ///将字符串后尾的'\0'去掉
            }
        }
        /// <summary>
        /// RC2加密处理类
        /// </summary>
        public static class RC2Helper
        {
            /// <summary>
            /// RC2加密
            /// </summary>
            /// <param name="encryptString">待加密的密文</param>
            /// <param name="encryptKey">密匙(必须为5-16位)</param>
            /// <returns></returns>
            public static string Encrypt(string encryptString, string encryptKey)
            {
                string returnValue;
                try
                {
                    byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                    RC2CryptoServiceProvider rC2 = new RC2CryptoServiceProvider();
                    byte[] byteEncryptString = Encoding.UTF8.GetBytes(encryptString);
                    MemoryStream memorystream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memorystream, rC2.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                    cryptoStream.Write(byteEncryptString, 0, byteEncryptString.Length);
                    cryptoStream.FlushFinalBlock();
                    returnValue = Convert.ToBase64String(memorystream.ToArray());

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return returnValue;

            }
            /// <summary>
            /// RC2解密
            /// </summary>
            /// <param name="decryptString">密文</param>
            /// <param name="decryptKey">密匙(必须为5-16位)</param>
            /// <returns></returns>
            public static string Decrypt(string decryptString, string decryptKey)
            {
                string returnValue;
                try
                {
                    byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                    RC2CryptoServiceProvider rC2 = new RC2CryptoServiceProvider();
                    byte[] byteDecrytString = Convert.FromBase64String(decryptString);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, rC2.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);
                    cryptoStream.Write(byteDecrytString, 0, byteDecrytString.Length);
                    cryptoStream.FlushFinalBlock();
                    returnValue = Encoding.UTF8.GetString(memoryStream.ToArray());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return returnValue;
            }

        }

        #endregion

        #region 非对称加密

        /// <summary>
        /// MD5加密处理类
        /// </summary>
        public static class MD5Helper
        {
            /// <summary>
            /// 字符串MD5加密
            /// </summary>
            /// <param name="inputedPassword"></param>
            /// <returns></returns>
            public static string Encryt(string inputedPassword)
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(inputedPassword, "MD5");
            }
            /// <summary>
            /// 密码比较
            /// </summary>
            /// <param name="inputedPassword"></param>
            /// <param name="currentPassword"></param>
            /// <returns></returns>
            public static bool Verify(string inputedPassword, string currentPassword)
            {
                string encryptedPassword = Encryt(inputedPassword);
                return (encryptedPassword == currentPassword.ToUpper()) ? true : false;
            }
        }

        /// <summary>
        /// SHA1加密处理类
        /// </summary>
        public static class SHA1Helper
        {
            /// <summary>
            /// 字符串SHA1加密
            /// </summary>
            /// <param name="inputedPassword"></param>
            /// <returns></returns>
            public static string Encryt(string inputedPassword)
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(inputedPassword, "SHA1");
            }
            /// <summary>
            /// 密码比较
            /// </summary>
            /// <param name="inputedPassword"></param>
            /// <param name="currentPassword"></param>
            /// <returns></returns>
            public static bool Verify(string inputedPassword, string currentPassword)
            {
                string encryptedPassword = Encryt(inputedPassword);
                return (encryptedPassword == currentPassword.ToUpper()) ? true : false;
            }
        }
        #endregion

        #region AES 加解密
        /// <summary>
        /// 有密码的AES加密 
        /// </summary>
        /// <param name="text">加密字符</param>
        /// <param name="password">加密的密码</param>
        /// <param name="iv">密钥</param>
        /// <returns></returns>
        public static string AESEncrypt(string text, string password, string iv)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();

            rijndaelCipher.Mode = CipherMode.CBC;

            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 128;

            rijndaelCipher.BlockSize = 128;

            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);

            byte[] keyBytes = new byte[16];

            int len = pwdBytes.Length;

            if (len > keyBytes.Length) len = keyBytes.Length;

            System.Array.Copy(pwdBytes, keyBytes, len);

            rijndaelCipher.Key = keyBytes;


            byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
            rijndaelCipher.IV = ivBytes;

            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

            byte[] plainText = Encoding.UTF8.GetBytes(text);

            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);

            return Convert.ToBase64String(cipherBytes);

        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="text"></param>
        /// <param name="password"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string AESDecrypt(string text, string password, string iv)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();

            rijndaelCipher.Mode = CipherMode.CBC;

            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 128;

            rijndaelCipher.BlockSize = 128;

            byte[] encryptedData = Convert.FromBase64String(text);

            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);

            byte[] keyBytes = new byte[16];

            int len = pwdBytes.Length;

            if (len > keyBytes.Length) len = keyBytes.Length;

            System.Array.Copy(pwdBytes, keyBytes, len);

            rijndaelCipher.Key = keyBytes;

            byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
            rijndaelCipher.IV = ivBytes;

            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();

            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

            return Encoding.UTF8.GetString(plainText);

        }

        /// <summary>
        /// 随机生成密钥
        /// </summary>
        /// <returns></returns>
        public static string GetIv(int n)
        {
            char[] arrChar = new char[]{
           'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
           '0','1','2','3','4','5','6','7','8','9',
           'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
          };

            StringBuilder num = new StringBuilder();

            Random rnd = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < n; i++)
            {
                num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());

            }

            return num.ToString();
        }
        #endregion
    }
}
